package com.mrjy.coursepackage.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mrjy.common.entity.CoursePackageCommentInfo;
import com.mrjy.custom.dto.CustomModuleDto;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.CoursePackageInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.coursepackage.bo.ICoursePackageInfoBo;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.maintain.util.RoleInfoEnum;
/**
 * 课程包基本信息 控制器
 * @author csshui
 *
 */
@Controller
@RequestMapping("/coursepackage/coursePackageInfo")
public class CoursePackageInfoController {	
	
	
	@Resource
	private ICoursePackageInfoBo coursePackageInfoBo;
	
	/**
	 * @author Javin
	 * @date 2017年9月18日
	 * @desc 添加课程包，关联标签表、视频表
	 * @param info
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(CoursePackageInfoDTO info,HttpSession session){
		return coursePackageInfoBo.txInsert(info,session);
	}
	/**
	 * @author Javin
	 * @date 2017年9月18日
	 * @desc 编辑后更新 for admin
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public Object updateById(CoursePackageInfoDTO info,HttpSession session){
		return coursePackageInfoBo.txUpdateById(info,session);
	}
	/**
	 * 根据id更新（更改所有值）
	 * @param info
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(CoursePackageInfo info){
		return coursePackageInfoBo.txUpdate(info);
	}
	
	/**
	 * @author Javin
	 * @date 2017年9月18日
	 * @desc 插入课程包ID， for admin
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public Object selectById(Integer id){
		return coursePackageInfoBo.selectById(id);
	}
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Integer id){
		return coursePackageInfoBo.txDelete(id);
	}
	/**
	 * 通过id来查询导师课程包基本信息
	 * @param info info.id 课程包id  
	 * @return
	 */
	@RequestMapping("/selectTeacherPackageByIdAndType")
	@ResponseBody
	public Object selectTeacherPackageByIdAndType(CoursePackageInfo info){
		return coursePackageInfoBo.selectTeacherPackageByIdAndType(info);
	}
	/**
	 * 通过id来查询主题课程包基本信息
     * @param info info.id 课程包id
     * @param session
     * @return
     */
	@RequestMapping("/selectThemePackageByIdAndType")
	@ResponseBody
    public Object selectThemePackageByIdAndType(CoursePackageInfo info, HttpSession session) {
        return coursePackageInfoBo.selectThemePackageByIdAndType(info, session);
    }
	/**
	 * 通过id查询视频标签
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectVideoTypeById")
	@ResponseBody
	public Object selectVideoTypeById(Integer id){
		return coursePackageInfoBo.selectVideoTypeById(id);
	}
	/**
	 * 主题课程包页 通过id查询导师的头像
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectTeacherHeadImgById")
	@ResponseBody
	public Object selectTeacherHeadImgById(Integer id){
		return coursePackageInfoBo.selectTeacherHeadImgById(id);
	}
	/**
	 * 通过用户id查询我的课程，里面包括了判断是否是会员的操作及相应的操作(分页)
	 * @param userId(用户id，非老师id，这里只是复用了此字段), page , rows
	 * @return
	 */
	@RequestMapping("/selectMyCoursePackageByUserId")
	@ResponseBody
	public Object selectMyCoursePackageByUserId(CoursePackageInfo info){
		return coursePackageInfoBo.selectMyCoursePackageByUserId(info);
	}
	/**
	 * 根据课程包类型查询 导师包列表或主题包列表
	 * @param info info.coursePackageType  0:主题课程包;1:导师课程包 ，info.page info.rows 没传就是查全部
	 * @return 
	 */
	@RequestMapping("/selectPageByCoursePackageType")
	@ResponseBody
	public Object selectPageByCoursePackageType(CoursePackageInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo != null){
			info.setUserId(userInfo.getId());
		}
		return coursePackageInfoBo.selectPageByCoursePackageType(info,info.getUserId());
	}
	
	/**
	 * 通过课程包id查询其包含的视频，支持分页查询
	 * @param info info.id , info.page, info.rows
	 * @return
	 */
	@RequestMapping("/selectVideoListById")
	@ResponseBody
    public Object selectVideoListById(CoursePackageInfo info, HttpSession session) {
        return coursePackageInfoBo.selectVideoListById(info, session);
    }
	/**
	 * 批量删除
	 * @param ids
	 * @param session
	 * @return
	 */
	@RequestMapping("/deleteByIds")
	@ResponseBody
	public Object deleteByIds(int[] ids, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (userInfo.getRoleId().intValue() == RoleInfoEnum.SYS_ADMIN.getRoleId() 
				|| userInfo.getRoleId().intValue() == RoleInfoEnum.NORMAL_ADMIN.getRoleId()) {
			return coursePackageInfoBo.txDeleteByIds(ids);
		} else {
			return "Forbidden";
		}
	}

	/**
	 * @author Javin
	 * @date 2017年9月18日
	 * @desc 启用禁用，支持批量 for admin
	 * @param info
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(CoursePackageInfo info, HttpSession session) {
	/*	UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (userInfo.getRoleId() == 1 || userInfo.getRoleId() == 5 || userInfo.getRoleId() == 6) {
			return coursePackageInfoBo.txUpdateDisableFlag(info);
		} else {
			return "Forbidden";
		}*/
		return coursePackageInfoBo.txUpdateDisableFlag(info,session);
	}
	/**
	 * 通过id和类型来查询
	 * @param info info.id 课程包id  
	 * @return
	 * @author Javin isUse = true
	 */
	@RequestMapping("/selectCoursePackageByIdAndType")
	@ResponseBody
	public Object selectCoursePackageByIdAndType(CoursePackageInfo info){
		return coursePackageInfoBo.selectCoursePackageByIdAndType(info);
	}
	
	
	/**
	 * 根据课程包类型查询 导师包列表或主题包列表 (后台使用 )
	 * @param info info.coursePackageType  0:主题课程包;1:导师课程包 ，info.page info.rows 没传就是查全部
	 * @return
	 */
	@RequestMapping("/selectPageByCoursePackageTypeForBack")
	@ResponseBody
	public Object selectPageByCoursePackageTypeForBack(CoursePackageInfoDTO info,HttpSession session){
		return coursePackageInfoBo.selectPageByCoursePackageTypeForBack(info,session);
	}
	/**
	 * 根据教师的id查询所有的导师课程包
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/queryAllTeacherPackage")
	@ResponseBody
	public Object queryAllTeacherPackage(Integer teacherId,HttpSession session){
        return coursePackageInfoBo.queryAllTeacherPackage(teacherId, session);
    }
	
	/**
	 * 根据教师的id查询所有的导师课程包 for back
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/queryTeacherPackageByTeacherId")
	@ResponseBody
	public Object queryTeacherPackageByTeacherId(Integer teacherId){
		return coursePackageInfoBo.queryTeacherPackageByUserId(teacherId);
	}
	
	/**
	 * 根据教师的id查询该导师所有的主题课程包
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/queryAllThemePackage")
	@ResponseBody
	public Object queryAllThemePackage(Integer teacherId,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return coursePackageInfoBo.queryAllThemePackage(teacherId,userInfo==null?null:userInfo.getId());	
	}
	
	/**
	 * 导出excel for back
	 * @param info
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(CoursePackageInfoDTO info,HttpServletResponse response,HttpSession session){
		return coursePackageInfoBo.exportExl(info,response,session);
	}
	
	/**
	 * <微信1.5课堂改版>
	 * 根据标签 展示课程包  
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryByVideoType")
	@ResponseBody
	public Object queryByVideoType(CoursePackageInfoDTO info) {
		return coursePackageInfoBo.queryByVideoType(info);
	}
	
	/**
	 * <明人open1.3>
	 * 添加课程包, 新增阶段下拉选择框
	 * 加载阶段下拉选择框
	 * @param info
	 * @return
	 */
	@RequestMapping("/queryStageTabInfo")
	@ResponseBody
	public Object queryStageTabInfo() {
		return coursePackageInfoBo.queryStageTabInfo();
	}

    /**
     * 分页 根据话题id(custom_module表的id) 查询包含话题标签的课程包
     *
     * @param info
     * @return
     */
    @RequestMapping("/queryCourseByCustomModuleId")
    @ResponseBody
    public Object queryCourseByCustomModuleId(CoursePackageInfoDTO coursePackageInfoDTO) {
        return coursePackageInfoBo.queryCourseByCustomModuleId(coursePackageInfoDTO);
    }

    /**
     * 根据videoId查询所属特价课程包
     * @param info
     * @return
     */
    @RequestMapping("/queryBelongSpecialCP")
    @ResponseBody
    public Object queryBelongSpecialCP(Integer videoId) {
        return coursePackageInfoBo.queryBelongSpecialCP(videoId);
    }
    
    /**
     * 根据标签查询相关课程(课程包)
     * @param userId
     * @param types 格式:8,23,24,125 字符串逗号分隔
     * @return
     */
    @RequestMapping(value="list",method=RequestMethod.GET)
    @ResponseBody
    public Object list(String types
    		,@RequestParam Integer page
    		,@RequestParam Integer rows,HttpSession session) {
        return coursePackageInfoBo.queryPageByType(types,page,rows,session);
    }
    


    /**
     * 通过课程包id查询其包含的视频，支持分页查询
     *
     * @param info info.id , info.page, info.rows
     * @return
     */
    @RequestMapping("/selectUnFinishVideoListById")
    @ResponseBody
    public Object selectUnFinishVideoListById(CoursePackageInfo info) {
        return coursePackageInfoBo.selectUnFinishVideoListById(info);
    }
}
